<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<ul id="deptTree" class="ztree"></ul>
<script type="text/javascript">
	$( function() {
		var ctxPath = "${pageContext.request.contextPath}";
		var zNodes = '${treeNodes}';
		zNodes = util.strToObj(zNodes);

		//点击时执行
		var zTreeOnClick = function(event, treeId, treeNode) {
			var id = null;
			if (treeNode && !(treeNode.parentTId == 0 || treeNode.parentTId == null)) {
				id = treeNode.depId;
			}
			var fun = ${callback};
			fun(id);
		};

		//删除节点回调函数
		var delCallBack = function(treeNode, modal) {
			$.ajax({
			type : "POST",
			async : false,
			url : ctxPath + "/user/dept!delMulti.action",
			data : "id=" + treeNode.depId,
			success : function(msg) {
				modal.modal('hide');
				msg = util.strToObj(msg);
				if (msg.code == "success") {
					zTreeOnClick();
					zTree.removeChildNodes(treeNode);
					zTree.removeNode(treeNode);
					zTree.reAsyncChildNodes(null, "refresh");
					dialog.calloutAlert(msg.desc,"","success");
				}else
					dialog.calloutAlert(msg.desc,"","error");

			}
			});
		};

		//节点删除前事件处理
		var beforeRemove = function(treeId, treeNode) {
			var str = "该部门人员也会被删除，确定删除？";
			if (treeNode.isParent) {
				str = "该节点下还有子项，该节点和各子项人员也会被删除，仍要删除吗？";
			}
			dialog.confirm(treeNode, delCallBack, '', str);
			return false;
		};

		//处理是否显示删除按钮
		var showRemoveBtn = function(treeId, treeNode) {
			if (treeNode.parentTId == 0 || treeNode.parentTId == null) {
				return false;
			} else {
				return true;
			}
		};

		//鼠标移到树节点上处理的事件
		var addHoverDom = function(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
				return;

			//初始化添加子节点事件
			var urladd = ctxPath + "/user/dept!input.action?parentId=" + treeNode.depId + "&isAdd=true";
			if (treeNode.parentTId == 0 || treeNode.parentTId == null) {
				//--根节点
				urladd = ctxPath + "/user/dept!input.action?isAdd=true";
			}
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加子部门' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btnAdd = $("#addBtn_" + treeNode.tId);
			if (btnAdd)
				btnAdd.bind("click", function() {
					dialog.formModal(urladd, "新增子部门", "400");
				});
			//初始化节点编辑事件
			var urledit = ctxPath + "/user/dept!input.action?id=" + treeNode.depId + "&isAdd=false";
			var titleEdit = "修改部门信息";
			if (treeNode.parentTId == 0 || treeNode.parentTId == null) {
				//--根节点
				urledit = ctxPath + "/user/com!input.action?id=" + treeNode.depId;
				titleEdit = "修改企业信息";
			}
			var btnedit = $("#" + treeNode.tId + "_edit");
			if (btnedit)
				btnedit.bind("click", function() {
					dialog.formModal(urledit, titleEdit, '400');
				});
		};

		//鼠标移出节点时处理的事件
		var removeHoverDom = function(treeId, treeNode) {
			$("#addBtn_" + treeNode.tId).unbind().remove();
			$("#" + treeNode.tId + "_edit").unbind().remove();
		};

		//树节点点击前触发时间
		var beforeClick = function(treeId, treeNode) {
			$("#addBtn_" + treeNode.tId).unbind().remove();
			$("#" + treeNode.tId + "_edit").unbind().remove();
		};

		//检测是否执行了拖拽
		var beforeDrag = function(treeId, treeNodes) {
			for (var i = 0, l = treeNodes.length; i < l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		};

		//drop：终止，拖拽终止前查看是否允许该拖拽
		var beforeDrop = function(treeId, treeNodes, targetNode, moveType) {
			var flag = targetNode ? targetNode.drop !== false : false;
			message = targetNode ? "目标节点不能为空" : "目标节点不允许拖拽！";
			if (flag) {
				var node = treeNodes[0];
				var isCom = false;

				if (targetNode.parentTId == 0 || targetNode.parentTId == null) {
					//--根节点
					isCom = true;
					if (moveType !== 'inner') {
						flag = false;
						message = "不能创建企业！";
					}
				}
			}
			if (flag) {
				$.ajax({
					type : "POST",
					async : false,
					url : ctxPath + "/user/dept!toModify.action",
					data : "id=" + node.depId + "&depName=" + node.depName + "&depId=" + targetNode.depId + "&type=" + moveType + "&isCom=" + isCom,
					success : function(msg) {
						//--根据结果判断可否拖拽
						if (msg.trim() == 'false') {
							flag = false;
						}
					}
				});

				if (!flag) {
					message = '"' + targetNode.depName + '"已存在"' + node.depName + '"子部门';
					if (moveType !== 'inner') {
						message = '"' + targetNode.getParentNode().depName + '"已存在"' + node.depName + '"子部门';
					}
				}
			}
			if (!flag)
				dialog.calloutAlert(message,"","warning");
			return flag;
		};

		var onDrop = function(event, treeId, treeNodes, targetNode, moveType, isCopy) {

			//"inner"：成为子节点，"prev"：成为同级前一个节点，"next"：成为同级后一个节点,如果 moveType = null，表明拖拽无效
			/* 如果目标和原父节点一样或和自身一样，不执行 */
			var node = treeNodes[0];
			var isCom = false;
			if (targetNode.parentTId == 0 || targetNode.parentTId == null) {
				//--根节点
				isCom = true;
			}
			$.ajax({
				type : "POST",
				url : ctxPath + "/user/dept!modify.action?deleteFlag=false",
				data : "id=" + node.depId + "&depId=" + targetNode.depId + "&type=" + moveType + "&isCom=" + isCom,
				success : function(msg) {
					if(msg.code=="success")
						dialog.calloutAlert(msg.desc,"","success");
					else
						dialog.calloutAlert(msg.desc,"","error");
				}
			});
		};

		var ztreeClick = function(id) {
			var href = ctxPath + '/user/user!list.action';
			if (typeof id === 'string') {
				href += '?deptId=' + id;
			}
			util.ajaxPage(href, '', true, '#user-list');
		};

		//设置参数 初始化
		var setting = {
			view : {
				addHoverDom : addHoverDom,
				removeHoverDom : removeHoverDom,
				selectedMulti : false,
			},
			edit : {
				drag : {
					isCopy : false
				},
				enable : true,
				editNameSelectAll : false,
				removeTitle : "删除部门",
				renameTitle : "修改部门",
				showRemoveBtn : showRemoveBtn,
				showRenameBtn : true
			},
			data : {
				simpleData : {
					enable : false
				},
				key : {
				children : "children",
				id : "depId",
				name : "depName",
				title : "fullName"
				}
			},
			callback : {
				beforeDrag : beforeDrag,
				beforeRemove : beforeRemove,
				beforeRename : false,
				beforeDrop : beforeDrop,
				beforeClick : beforeClick,
				onClick : zTreeOnClick,
				onDrop : onDrop
			}
		};

		var zTree = $.fn.zTree.init($("#deptTree"), setting, zNodes);
		zTree.expandNode(zTree.getNodes()[0], true, false, false);
	});
</script>

